package com.example.dragon.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.dragon.bean.AdressBean;
import com.example.dragon.xmf.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dragon on 2017/6/24.
 */

public class AddressAdpter extends RecyclerView.Adapter {
    private List<AdressBean> addressBeanList = new ArrayList<>();
    Context mcontext;
    public AddressAdpter(Context mcontext,List<AdressBean> addressBeanList) {
        this.mcontext = mcontext;
        this.addressBeanList=addressBeanList;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mcontext).inflate(R.layout.adress_item_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AdressBean item = addressBeanList.get(position);

        final ViewHolder viewHolder = (ViewHolder) holder;
        if (item != null) {
            viewHolder.adStreeText.setText(item.getStreet());
            // viewHolder.isdnameText.setText(item.getDname());
            viewHolder.adStreeenumText.setText(item.getStreetNum());
        }
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return addressBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView adStreeText,adStreeenumText;
        public ViewHolder(View itemView) {
            super(itemView);
            adStreeText =(TextView)itemView.findViewById(R.id.streenameText);
            adStreeenumText =(TextView)itemView.findViewById(R.id.stressText);
        }
    }
}
